/*
 * Pane.h
 *
 *  Created on: 2011-3-1
 *      Author: hao
 */

#ifndef PANE_H_
#define PANE_H_

#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>
#include <functional>
#include "DecoderManager.h"
#include "Error.h"
#include <string>
#include <vector>
#include "StreamEndFactroy.h"
#include "StreamOperationFactroy.h"
#include "DecoderFactroy.h"
#include "StreamOperationSlot.h"
#include "CueParser.h"
#include "DecoderManager.h"
#include "DecoderPipleLine.h"
#include "PluginCtrlPane.h"

using std::vector;

class Pane {
    static Pane * pSelf;

    DecoderPipleLine callback;

    bool SetupFile(const char * path);

    bool UnsetFile();
    int fileId;

    void * pData;

    bool bSetup;

    struct stat fileStat;

    std::function<void() > playEndCallback;

    DecoderMaster Master;

    vector< string > pluginPath;

    vector< int > StreamEndIndex;

    vector< int > DecoderIndex;

    vector< int > StreamOperationIndex;
    
    vector< PluginCtrlPane * > vPluginPane;
    
    PluginCtrlPane * EQPane;

    StreamTerminal * pTerminal;

    StreamOperationSlot pinSlot;

    StreamEndFactroy streamEndFactroy;

    StreamOperationFactroy streamOperationFactroy;

    DecoderFactroy decoderFactroy;

    CueParser cueparser;

    static void drainCallback();

public:

    Pane();

    ~Pane();

    void CheckPlugin();

    void SetupPlugin();

    void PlayingFile(const char * szPath, int * pMin = NULL, int * pSecond = NULL, int * pFrame = NULL);

    void PlayingTrack(const CueSheet & sheet, unsigned TrackIndex, int * pMin = NULL, int * pSecond = NULL, int * pFrame = NULL);

    void setVolumeDB(double db);

    void setVolumeLinear(double linear);

    void setChannelVolumeDB(int channel, double db);

    void setChannelVolumeLinear(int channel, double linear);

    bool Seek(int min, int second, int frame);

    void Pause();

    void Stop();

    void Resume();

    bool isSetup();

    bool strRecoding(const char * str, char * buffer, size_t bufferlen);
    bool CodeConver( const char * toCharSet, const char * fromCharSet, const char * str, char * buffer, size_t bufferlen );

    CueSheet AnalysisCue(const char * szPath);

    std::string getSurportPrefixName();

    double getCurrentTime();
    
    PluginCtrlPane * getEQPane(){
        return EQPane;
    }
    
    unsigned getTotalPluginCtrlPane(){
        return vPluginPane.size();
    }
    
    PluginCtrlPane * getPluginCtrlPane( unsigned index ){
        if( index >= vPluginPane.size() )
            return NULL;
        return vPluginPane[index];
    }

    //create the playing callback;
    bool createCallback(unsigned streamendIndex);

    void setPalyEndCallback(const std::function<void()> & fun) {
        playEndCallback = fun;
    }

};


#endif /* PANE_H_ */
